package com.example.user.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.example.model.user.Menu;
import com.example.model.user.RoleMenu;
import com.example.model.user.vo.MetaVo;
import com.example.model.user.vo.RouterVo;
import com.example.user.mapper.MenuMapper;
import com.example.user.mapper.RoleMenuMapper;
import com.example.user.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public int delete(Long id) {
        roleMenuMapper.delete(id);
        return menuMapper.delete(id);
    }

    @Override
    public int insert(Menu record) {
        return menuMapper.insert(record);
    }

    @Override
    public Menu get(Long id) {
        return menuMapper.get(id);
    }

    @Override
    public int insertRoleMenu(List<RoleMenu> records){
        Long roleId = records.get(0).getRoleId();
        roleMenuMapper.delete(roleId);
        return roleMenuMapper.insertBatch(records);
    }

    @Override
    public int update(Menu record) {
        return menuMapper.update(record);
    }

    @Override
    public List<Menu> list(Menu menu) {
        List<Menu> menus = menuMapper.list(menu);
        return buildTree(menus);
//        return menus;
    }

    @Override
    public List<Menu> listMenu() {
        List<Menu> menus = menuMapper.listMenu();
        return buildTree(menus);
    }

    @Override
    public List<Menu> listAll(Menu menu){
        List<Menu> menus = menuMapper.listMenuAll();
        return menus;
    }

    @Override
    public List<Menu>  listMenuByUserId(Long userId) {
        if (userId == 1L) {
            return list(null);
        }
        return buildTree(menuMapper.listMenuByUserId(userId));
    }

    @Override
    public List<Menu> listMenuByRoleId(Long roleId) {
        if (roleId == 1L) {
            return listAll(null);
        }
        return menuMapper.listMenuByRoleId(roleId);
    }




//    @Override
//    public int update(Menu record) {
//        return menuMapper.update(record);
//    }

    @Override
    public Set<String> getMenuPermsByUserId(Long userId) {
        List<String> list;
        if (userId == 1L) {
            list = menuMapper.getMenuPerms();
        } else {
            list = menuMapper.getMenuPermsByUserId(userId);
        }
        return list.stream().filter(StrUtil::isNotEmpty).collect(Collectors.toSet());
    }

    @Override
    public List<String> getMenuPermsByRoleId(Long roleId) {
        return menuMapper.getMenuPermsByRoleId(roleId);
    }

    @Override
    public List<RouterVo> getRouters(Long userId) {
        List<Menu> menus = listMenuByUserId(userId);
        return buildRouters(menus);
    }

    private List<Menu> buildTree(List<Menu> originMenu) {
        List<Menu> headMenu = new ArrayList<>();
        for (Menu menu : originMenu) {
            for (Menu e : originMenu) {
                if (menu.getMenuId().equals(e.getParentId())) {
                    menu.getChildren().add(e);
                }
            }
            if (menu.getParentId() == 0) {
                headMenu.add(menu);
            }
        }
        return headMenu;
    }

    public List<RouterVo> buildRouters(List<Menu> menus) {
        List<RouterVo> routerVos = menus.stream().map(menu -> {
            RouterVo routerVo = new RouterVo();
            routerVo.setPath(this.getRouterPath(menu));
            routerVo.setHidden("1".equals(menu.getVisible()));
            routerVo.setComponent(this.getRouterComponent(menu));
            String path = menu.getPath();
            // system -> System
            String name = path.substring(0, 1).toUpperCase() + path.substring(1);
            routerVo.setName(name);
            MetaVo meta = new MetaVo();
            meta.setTitle(menu.getMenuName());
            meta.setIcon(menu.getIcon());
            routerVo.setMeta(meta);
            if (!menu.getChildren().isEmpty() && "M".equals(menu.getMenuType())) {
                routerVo.setRedirect("noRedirect");
                // 子节点调用当前方法进行再次转换
                routerVo.setChildren(buildRouters(menu.getChildren()));
            }
            return routerVo;
        }).toList();
        return routerVos;
    }

    private String getRouterPath(Menu menu)
    {
        String routerPath = menu.getPath();
        if (0 == menu.getParentId().intValue() && "M".equals(menu.getMenuType())) {
            routerPath = "/" + menu.getPath();
        }
        return routerPath;
    }

    private String getRouterComponent(Menu menu)
    {
        String component = "Layout";
        if (StrUtil.isNotEmpty(menu.getComponent()) && "C".equals(menu.getMenuType())) {
            component = menu.getComponent();
        }
        return component;
    }


}
